/**
  * Qt adaptation for our game code.
  * Initialises game (game_init, game_prepare) and periodically calls game_update and
  * game_render if the application is visible.
  * If the application is hidden, game_release is called in order to free resources for other applications
  */

#include "vgwidget.h"
#include "game.h"
#include <QDebug>

VGWidget::VGWidget(QWidget *parent)
    : QWidget(parent)
{
    t0 = QTime::currentTime();
    tt = t0;

    frames = 0;
    game_prepared=0;
    game_init();
    setAttribute( Qt::WA_AcceptTouchEvents, true );
}


VGWidget::~VGWidget()
{
    if(game_prepared)
    {
        game_prepared=false;
        game_release();
    }
    game_destroy();
}

bool VGWidget::event(QEvent *event)
{
    switch(event->type())
    {
    case QEvent::TouchBegin:
    case QEvent::TouchEnd:
    case QEvent::TouchUpdate:

        {
        //qDebug("event->type %d", event->type());
            QList<QTouchEvent::TouchPoint> touchPoints = static_cast<QTouchEvent *>(event)->touchPoints();
            foreach (const QTouchEvent::TouchPoint &touchPoint, touchPoints)
            {
                QPoint p = touchPoint.pos().toPoint();
                game_event(touchPoint.state(), p.x(), p.y(), touchPoint.id());
            }
        }
        return true;
        break;
    case QEvent::WindowActivate:
        qDebug("WindowActivate");
        setUpdatesEnabled(true);
        repaint(); // TODO update() didnt work here, investigate
        break;
    case QEvent::WindowDeactivate:
        qDebug("WindowDeactivate");
        if(game_prepared)
        {
            qDebug("game_release");
            game_prepared=false;
            game_release();
        }
        setUpdatesEnabled(false);
        break;
    }

    QWidget::event(event);
}

void VGWidget::paintEvent(QPaintEvent *)
{
    //qDebug("paintevent");
    //vgFlush();
    QTime now = QTime::currentTime();
//#ifndef NDEBUG
    int dt=tt.msecsTo(now);
    if(frames>100)// || dt>5000)
    {
        if(dt)
        {
            message = QString("fps=%1").arg(frames*1000.0f/dt, 0, 'f', 3);
            qDebug() << message;
        }

        tt=now;
        frames=0;
    }
//#endif


    game_update(t0.msecsTo(now)/1000.0f);
    frames++;

    QPainter painter;
    painter.begin(this);
    painter.setRenderHints(QPainter::Antialiasing);


    if (painter.paintEngine()->type() == QPaintEngine::OpenVG)
    {
        painter.beginNativePainting();

        if(!game_prepared)
        {
            qDebug("game_prepare");
            game_prepared=true;
            game_prepare();
        }

        game_render();
        painter.endNativePainting();
    }
    else
    {
        qDebug("paintEvent skipped, not using OpenVG");
    }

#ifndef NDEBUG
    vgFlush();
    int frametime = now.msecsTo(QTime::currentTime());

    painter.setPen(Qt::white);
    painter.setFont(QFont("Arial", 10));
    painter.drawText(QPointF(10, 30), message);


    int y=(frametime)*360/33;

    painter.drawRect(630, 359-y, 1, y);

#endif
    painter.end();
    update();
}
